package com.sunplus.system.registration.service.impl;

import java.util.Date;
import java.util.List;

import com.sunplus.common.utils.uuid.UUID;
import com.sunplus.system.registration.domain.PortalMouldField;
import com.sunplus.system.registration.domain.PortalRecordDetails;
import com.sunplus.system.registration.mapper.PortalRecordDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sunplus.system.registration.mapper.PortalRecordMapper;
import com.sunplus.system.registration.domain.PortalRecord;
import com.sunplus.system.registration.service.IPortalRecordService;

import javax.annotation.Resource;

/**
 * 记录Service业务层处理
 *
 * @author sunplus
 * @date 2021-03-25
 */
@Service
public class PortalRecordServiceImpl implements IPortalRecordService {
    @Autowired
    private PortalRecordMapper portalRecordMapper;
    @Autowired
    private PortalRecordDetailsMapper portalRecordDetailsMapper;

    /**
     * 查询记录
     *
     * @param pid 记录ID
     * @return 记录
     */
    @Override
    public PortalRecord selectPortalRecordById(String pid) {
        PortalRecord portalRecord = portalRecordMapper.selectPortalRecordById(pid);
        if(portalRecord!=null&&portalRecord.getPID()!=null){
            PortalRecordDetails portalRecordDetails = new PortalRecordDetails();
            portalRecordDetails.setRecordPid(portalRecord.getPID());
            List<PortalRecordDetails> portalRecordDetailsList = portalRecordDetailsMapper.selectPortalRecordDetailsList(portalRecordDetails);
            portalRecord.setRecordDetailsList(portalRecordDetailsList);
        }
        return portalRecord;
    }

    /**
     * 查询记录列表
     *
     * @param portalRecord 记录
     * @return 记录
     */
    @Override
    public List<PortalRecord> selectPortalRecordList(PortalRecord portalRecord) {
        return portalRecordMapper.selectPortalRecordList(portalRecord);
    }

    /**
     * 新增记录
     *
     * @param portalRecord 记录
     * @return 结果
     */
    @Override
    public int insertPortalRecord(PortalRecord portalRecord) {
        String pid = UUID.randomUUID().toString();
        portalRecord.setPID(pid);
        portalRecord.setCreateTime(new Date());
        portalRecord.setCreateUser("app");
        portalRecordMapper.insertPortalRecord(portalRecord);
        for (PortalRecordDetails portalRecordDetails : portalRecord.getRecordDetailsList()) {
            portalRecordDetails.setRecordPid(pid);
            portalRecordDetails.setPID(UUID.randomUUID().toString());
            portalRecordDetails.setCreateTime(new Date());
            portalRecordDetails.setCreateUser("app");
            portalRecordDetailsMapper.insertPortalRecordDetails(portalRecordDetails);
        }
        return 1;
    }

    /**
     * 修改记录
     *
     * @param portalRecord 记录
     * @return 结果
     */
    @Override
    public int updatePortalRecord(PortalRecord portalRecord) {
        return portalRecordMapper.updatePortalRecord(portalRecord);
    }

    /**
     * 批量删除记录
     *
     * @param pids 需要删除的记录ID
     * @return 结果
     */
    @Override
    public int deletePortalRecordByIds(String[] pids) {
        return portalRecordMapper.deletePortalRecordByIds(pids);
    }

    /**
     * 删除记录信息
     *
     * @param pid 记录ID
     * @return 结果
     */
    @Override
    public int deletePortalRecordById(String pid) {
        return portalRecordMapper.deletePortalRecordById(pid);
    }
}
